package com.example.autumn.controller.portal;


import com.example.autumn.config.NacosPatternConfig;
import com.example.autumn.entity.User;
import com.example.autumn.service.IUserService;
import com.example.common.response.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Liao run
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    NacosPatternConfig patternConfig;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/now")
    @ApiOperation("测试nacos的配置读取功能")
    public String now(@RequestHeader(required = false, value = "Truth") String truth) {

        System.out.println(truth);
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternConfig.dateformat));
    }


    @GetMapping("/all")
    @ApiOperation("获取nacos所有的时间格式配置")
    public NacosPatternConfig getPatternConfig() {
        return patternConfig;
    }

    @PostMapping("/register")
    @ApiOperation("注册用户")
    public R register(@RequestBody User user) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.set("test", "1");
        redisTemplate.expire("test", 120, TimeUnit.SECONDS);


        return userService.register(user);
    }


    @GetMapping("/{id}")
    public R<User> test(@PathVariable("id") Long id) {

        return userService.getUserById(id);
    }
}

